namespace Landscape.Business.Reporting
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;
    using Telerik.Reporting;
    using Telerik.Reporting.Drawing;
    using System.Data;
    using Landscape.Business.Common;

    /// <summary>
    /// Summary description for IncidentDetails.
    /// </summary>
    public partial class IncidentDetails : Telerik.Reporting.Report
    {
        public Guid IncidentID
        { get; set; }

        public IncidentDetails()
        {
            //
            // Required for telerik Reporting designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //

            
        }

        public IncidentDetails(Guid pIncID)
        {
            InitializeComponent();

            var oraGuid = GUIDField.CreateFromMicrosoftGuid(pIncID);
            //this.ReportParameters[0].Value = oraGuid.ToOracleString().ToUpper();

            string iDetCmd =
                @"SELECT EI.ID, EI.TITLE, (CASE WHEN EI.IS_INCIDENT = 1 THEN 'Incident' ELSE 'Dril' END) AS INCIDENT_TYPE, (CASE WHEN EI.IS_CLOSED = 1 THEN 'Closed' ELSE 'Open' END) AS IS_CLOSED, 
                (CASE WHEN EI.CLOSED_DATE IS NOT NULL THEN TO_CHAR(EI.CLOSED_DATE, 'DD-MON-YYYY') ELSE '-' END) CLOSED_DATE, (CASE WHEN EI.CREATED_DATE IS NOT NULL THEN TO_CHAR(EI.CREATED_DATE, 'DD-MON-YYYY') ELSE '-' END) CREATED_DATE,
                (UM1.FIRST_NAME || ' '|| UM1.LAST_NAME) AS CREATED_BY, (CASE WHEN EI.CLOSED_BY <>'00000000000000000000000000000000' THEN (TO_CHAR(UM2.FIRST_NAME) || ' '|| TO_CHAR(UM2.LAST_NAME)) ELSE '-' END) AS CLOSED_BY,
                V.NAME AS VSL_NAME, S.MESSAGE, S.SHORT_ITEM_NAME, (UM3.FIRST_NAME || ' '|| UM3.LAST_NAME) AS LOG_BY, (CASE WHEN S.CREATED_DATE IS NOT NULL THEN TO_CHAR(S.CREATED_DATE, 'DD-MON-YYYY HH:MM:SS') ELSE '-' END) LOG_TIME
                FROM EMERGENCY_INCIDENT EI, USER_MASTER UM1, USER_MASTER UM2, VESSEL V, SCRIBE S, USER_MASTER UM3
                WHERE UM1.ID = EI.CREATED_BY AND EI.CLOSED_BY = UM2.ID(+) AND V.ID = EI.VESSEL_ID AND S.EMERGENCYINCIDENT_ID = EI.ID AND UM3.ID = S.CREATED_BY AND EI.ID = '{0}'";
                        
            var qry = string.Format(iDetCmd, oraGuid.ToOracleString().ToUpper());
            this.IDet.SelectCommand = qry;
            this.DataSource = this.IDet;
        }

        void IncidentDetails_NeedDataSource(object sender, System.EventArgs e)
        {
            string iDetCmd =
                        @"SELECT EI.ID, EI.TITLE, (CASE WHEN EI.IS_INCIDENT = 1 THEN 'Incident' ELSE 'Dril' END) AS INCIDENT_TYPE, (CASE WHEN EI.IS_CLOSED = 1 THEN 'Closed' ELSE 'Open' END) AS IS_CLOSED, 
                        (CASE WHEN EI.CLOSED_DATE IS NOT NULL THEN TO_CHAR(EI.CLOSED_DATE, 'DD-MON-YYYY') ELSE '-' END) CLOSED_DATE, (CASE WHEN EI.CREATED_DATE IS NOT NULL THEN TO_CHAR(EI.CREATED_DATE, 'DD-MON-YYYY') ELSE '-' END) CREATED_DATE,
                        (UM1.FIRST_NAME || ' '|| UM1.LAST_NAME) AS CREATED_BY, (CASE WHEN EI.CLOSED_BY <>'00000000000000000000000000000000' THEN (TO_CHAR(UM2.FIRST_NAME) || ' '|| TO_CHAR(UM2.LAST_NAME)) ELSE '-' END) AS CLOSED_BY,
                        V.NAME AS VSL_NAME
                        FROM EMERGENCY_INCIDENT EI, USER_MASTER UM1, USER_MASTER UM2, VESSEL V
                        WHERE UM1.ID = EI.CREATED_BY AND EI.CLOSED_BY = UM2.ID(+) AND V.ID = EI.VESSEL_ID AND EI.ID = '{0}'";

            var oraGuid = GUIDField.CreateFromMicrosoftGuid(this.IncidentID);

            var qry = string.Format(iDetCmd, oraGuid.ToOracleString().ToUpper());
            this.IDet.SelectCommand = qry;

            //this.DataSource = this.IDet;

            string scribeCmd =
                @"SELECT S.MESSAGE, S.SHORT_ITEM_NAME, (UM1.FIRST_NAME || ' '|| UM1.LAST_NAME) AS CREATED_BY, (CASE WHEN S.CREATED_DATE IS NOT NULL THEN TO_CHAR(S.CREATED_DATE, 'DD-MON-YYYY HH:MM:SS') ELSE '-' END) CREATED_DATE
                    FROM SCRIBE S, USER_MASTER UM1 
                    WHERE S.EMERGENCYINCIDENT_ID = '{0}' AND UM1.ID = S.CREATED_BY
                    ORDER BY S.SCRIBE_INDEX DESC";

            //this.ScribeData.SelectCommand = string.Format(scribeCmd, oraGuid.ToOracleString().ToUpper());

            this.DataSource = this.IDet;
        }
    }
}